//
// Created by Jisam on 2024/7/28 19:20.
// solve d
#include <bits/stdc++.h>

#define PSI pair<string,int>
#define PII pair<int,int>
#define PDI pair<double,int>
#define PDD pair<double,double>
#define VVI vector<vector<int>>
#define VI vector<int>
#define VS vector<string>

#define PQLI priority_queue<int, vector<int>, less<int>>
#define PQGI priority_queue<int, vector<int>, greater<int>>
using u32 = unsigned;
using i64 = long long;
using u64 = unsigned long long;

using namespace std;
int ans = INT_MAX;
int flag = 0;

void search(int i, int n, int cur, int t, vector<vector<int>> &a, vector<int> &visi, vector<int> &visj) {

    if (ans == 0) {
        flag = 1;
        return;
    }
    if (flag) return;

    if (i == n) {
        cout << " cur " << cur << "\n";
        ans = min(ans, abs(t - cur));
    }
    int flag2 = 0;

    for (int j = 0; j < n && flag2 == 0; j++) {
        if (visi[j]) continue;
        visi[j] = 1;
        for (int k = 0; k < n; k++) {
            visj[k] = 1;
            cout << " i + 1 " << i + 1 << " a[j][k] " << a[j][k] << " " << cur + a[j][k] << "\n";
            if (abs(cur + a[j][k] - t) > ans) {
                flag = 1;
                flag2 = 1;
                break;
            }
            search(i + 1, n, cur + a[j][k], t, a, visi, visj);
            visj[k] = 0;
        }

        visi[j] = 0;
    }
}

void solve() {
    int n, m;
    cin >> n >> m;
    vector a(n, vector<int>(m, 0));
    vector visx(n, 0), visy(n, 0);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> a[i][j];
        }
    }
    for (int i = 0; i < n; i++) {
        sort(a[i].begin(), a[i].end());
    }
//    for (int i = 0; i < n; i++) {
//        for (int j = 0; j < n; j++) {
//            cout << a[i][j] <<" ";
//        }
//    }
    int t;
    cin >> t;
    search(0, n, 0, t, a, visx, visy);
    cout << ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
//    cin >> T;
    while (T--) solve();
//    cout << "\n"[];
    return 0;
}